// 2. This do-file contains code for creating the main figures (Figures 1-5) and tables (Table 1) for the manuscript. 

**** Set globals 
set more off
clear all

global dir_data "[FILL IN]" // FILL IN WITH FOLDER PATH FOR MAIN FILES
global temp "`c(tmpdir)'"

cd "${dir_data}"

use "reg_estimates", clear

*******************
*****Figure 1******
*******************
**** Set globals for titles and file names 
global thmktinc_pov_ytitle "% Change in Market Income-to-Poverty Ratio"
global thallinc_pov_ytitle "% Change in Post-Transfer Income-to-Poverty Ratio"

global thmktinc_pov_fig1 "Figure 1a"
global thallinc_pov_fig1 "Figure 1b"	

**** Figures 
foreach var in thmktinc_pov thallinc_pov {
	twoway (rarea ci95ub_`var'_snap ci95lb_`var'_snap quarters, ///
		fi(inten50) lw(none) color(navy%30)) ///
		(line b_`var'_snap quarters, lcolor(navy)) ///
		(scatter b_`var'_snap_sig quarters, ///
		lcolor(navy) mcolor(navy) msymbol(circle)) ///
		(scatter b_`var'_snap_nosig quarters, ///
		lcolor(navy) mcolor(navy) msymbol(circle_hollow)) ///
		(scatter b_`var'_snap_blank quarters, ///
		lcolor(navy) mcolor(navy) msize(large) msymbol(circle)) ///		
		(rarea ci95ub_`var'_med ci95lb_`var'_med quarters, ///
		fi(inten50) lw(none) color(orange_red%30)) ///
		(line b_`var'_med quarters, lcolor(orange_red)) ///
		(scatter b_`var'_med_sig quarters, ///
		lcolor(orange_red) mcolor(orange_red) msymbol(triangle)) ///
		(scatter b_`var'_med_nosig quarters, ///
		lcolor(orange_red) mcolor(orange_red) msymbol(triangle_hollow)) ///	
		(scatter b_`var'_med_blank quarters, ///
		lcolor(orange_red) mcolor(orange_red) msize(large) msymbol(triangle)) ///			
		(rarea ci95ub_`var'_ui ci95lb_`var'_ui quarters, ///
		fi(inten50) lw(none) color(forest_green%30)) ///
		(line b_`var'_ui quarters, lcolor(forest_green)) ///
		(scatter b_`var'_ui_sig quarters, ///
		lcolor(forest_green) mcolor(forest_green) msymbol(square)) ///
		(scatter b_`var'_ui_nosig quarters, ///
		lcolor(forest_green) mcolor(forest_green) msymbol(square_hollow)) ///	
		(scatter b_`var'_ui_blank quarters, ///
		lcolor(forest_green) mcolor(forest_green) msize(large) msymbol(square)) ///			
		if quarters>=-5 & quarters<=5, ///
		xline(0, lpattern(solid) lcolor(gs6)) yline(0, lpattern(solid) lcolor(gs6)) ///	
		xtitle("Quarters Since First Receiving Program") ///	
		ytitle("${`var'_ytitle}" "(Relative to 4 Quarters Pre-Receipt)") ///	
		ylabel(-.4(.1).1, grid gmax gmin) xlabel(-5(1)5) ///		
		legend(order(5 "SNAP (${`var'_snap_mean_round})" 10 "Medicaid (${`var'_med_mean_round})" ///
		15 "UI (${`var'_ui_mean_round})") row(1) pos(6) size(medsmall)) ///
		graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)
	graph export "${`var'_fig1}.png", replace	
}

*******************
*****Figure 2******
*******************
**** Scale estimates by pre-receipt mean 
foreach prog in di wc ui snap tanf med ssi wic {
	foreach var in thmktinc_pov thallinc_pov {
			global b_`var'_`prog'_sc = ///
				${b_`var'_`prog'_q5}/${`var'_`prog'_mean}
			global se_`var'_`prog'_sc = ///
				${se_`var'_`prog'_q5}/${`var'_`prog'_mean}					
	}
}

**** Set up coefficient plots 
foreach var in thmktinc_pov thallinc_pov {
	matrix input mat_`var'_sc = ///
		(${b_`var'_di_sc}, ${b_`var'_wc_sc}, ${b_`var'_ui_sc}, ${b_`var'_snap_sc}, ///	
		${b_`var'_tanf_sc}, ${b_`var'_med_sc}, ${b_`var'_ssi_sc}, ${b_`var'_wic_sc} \ ///		
		${se_`var'_di_sc}, ${se_`var'_wc_sc}, ${se_`var'_ui_sc}, ${se_`var'_snap_sc}, /// 			
		${se_`var'_tanf_sc}, ${se_`var'_med_sc}, ${se_`var'_ssi_sc}, ${se_`var'_wic_sc})		
}

**** Set globals for titles and file names 
global thmktinc_pov_fig2 "Figure 2a"
global thallinc_pov_fig2 "Figure 2b"	
			
**** Figures
foreach var in thmktinc_pov thallinc_pov {
	coefplot (mat(mat_`var'_sc), ciopts(lcolor(navy)) mcolor(navy) msymbol(circle)), ///
		se(2) xline(0, lcolor(gs8) lpattern(dash)) ///
		xtitle("Percent Change in Outcome (Relative to 4 Quarters Pre-Receipt)") ///
		xlabel(-.4(.2).4) xscale(range(-.45 .45)) ///		
		coeflabel(c1 = "DI (${`var'_di_mean_round})" ///
		c2 = "Workers Comp. (${`var'_wc_mean_round})" ///
		c3 = "UI (${`var'_ui_mean_round})" ///
		c4 = "SNAP (${`var'_snap_mean_round})" ///
		c5 = "TANF (${`var'_tanf_mean_round})" ///
		c6 = "Medicaid (${`var'_med_mean_round})" ///
		c7 = "SSI (${`var'_ssi_mean_round})" ///
		c8 = "WIC (${`var'_wic_mean_round})") legend(off) ///
		graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)
	graph export "${`var'_fig2}.png", replace		
}

*******************
*****Figure 3******
*******************
**** Set globals for titles and file names 
global notworking_any_ytitle "Share of Households with Any Not Working"
global disabled_any_ytitle "Share of Households with Any Disabled"

global disabled_any_file "Figure 3a"
global notworking_any_file "Figure 3b"

**** Figures 
foreach var in disabled_any notworking_any {
	twoway (rarea ci95ub_`var'_snap ci95lb_`var'_snap quarters, ///
		fi(inten50) lw(none) color(navy%30)) ///
		(line b_`var'_snap quarters, lcolor(navy)) ///
		(scatter b_`var'_snap_sig quarters, ///
		lcolor(navy) mcolor(navy) msymbol(circle)) ///
		(scatter b_`var'_snap_nosig quarters, ///
		lcolor(navy) mcolor(navy) msymbol(circle_hollow)) ///
		(scatter b_`var'_snap_blank quarters, ///
		lcolor(navy) mcolor(navy) msize(large) msymbol(circle)) ///			
		(rarea ci95ub_`var'_med ci95lb_`var'_med quarters, ///
		fi(inten50) lw(none) color(orange_red%30)) ///
		(line b_`var'_med quarters, lcolor(orange_red)) ///
		(scatter b_`var'_med_sig quarters, ///
		lcolor(orange_red) mcolor(orange_red) msymbol(triangle)) ///
		(scatter b_`var'_med_nosig quarters, ///
		lcolor(orange_red) mcolor(orange_red) msymbol(triangle_hollow)) ///	
		(scatter b_`var'_med_blank quarters, ///
		lcolor(orange_red) mcolor(orange_red) msize(large) msymbol(triangle)) ///			
		(rarea ci95ub_`var'_ui ci95lb_`var'_ui quarters, ///
		fi(inten50) lw(none) color(forest_green%30)) ///
		(line b_`var'_ui quarters, lcolor(forest_green)) ///
		(scatter b_`var'_ui_sig quarters, ///
		lcolor(forest_green) mcolor(forest_green) msymbol(square)) ///
		(scatter b_`var'_ui_nosig quarters, ///
		lcolor(forest_green) mcolor(forest_green) msymbol(square_hollow)) ///	
		(scatter b_`var'_ui_blank quarters, ///
		lcolor(forest_green) mcolor(forest_green) msize(large) msymbol(square)) ///			
		if quarters>=-5 & quarters<=5, ///
		xline(0, lpattern(solid) lcolor(gs6)) yline(0, lpattern(solid) lcolor(gs6)) ///	
		xtitle("Quarters Since First Receiving Program") ///	
		ytitle("${`var'_ytitle}" "(Relative to 4 Quarters Pre-Receipt)") ///	
		ylabel(, grid gmax gmin) xlabel(-5(1)5) ///		
		legend(order(5 "SNAP (${`var'_snap_mean_round})" 10 "Medicaid (${`var'_med_mean_round})" ///
		15 "UI (${`var'_ui_mean_round})") row(1) pos(6) size(medsmall)) ///
		graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)
	graph export "${`var'_file}.png", replace	
}

*******************
*****Figure 4******
*******************
**** Set globals for titles and file names 
global kid_0to4_any_ytitle "Share of Households with Child Age 0-4"
global sepdivwid_any_ytitle "Share of Households with Any Separated"

global kid_0to4_any_file "Figure 4a"
global sepdivwid_any_file "Figure 4b"

**** Figures 
foreach var in kid_0to4_any sepdivwid_any {
	twoway (rarea ci95ub_`var'_snap ci95lb_`var'_snap quarters, ///
		fi(inten50) lw(none) color(navy%30)) ///
		(line b_`var'_snap quarters, lcolor(navy)) ///
		(scatter b_`var'_snap_sig quarters, ///
		lcolor(navy) mcolor(navy) msymbol(circle)) ///
		(scatter b_`var'_snap_nosig quarters, ///
		lcolor(navy) mcolor(navy) msymbol(circle_hollow)) ///
		(scatter b_`var'_snap_blank quarters, ///
		lcolor(navy) mcolor(navy) msize(large) msymbol(circle)) ///			
		(rarea ci95ub_`var'_med ci95lb_`var'_med quarters, ///
		fi(inten50) lw(none) color(orange_red%30)) ///
		(line b_`var'_med quarters, lcolor(orange_red)) ///
		(scatter b_`var'_med_sig quarters, ///
		lcolor(orange_red) mcolor(orange_red) msymbol(triangle)) ///
		(scatter b_`var'_med_nosig quarters, ///
		lcolor(orange_red) mcolor(orange_red) msymbol(triangle_hollow)) ///	
		(scatter b_`var'_med_blank quarters, ///
		lcolor(orange_red) mcolor(orange_red) msize(large) msymbol(triangle)) ///			
		(rarea ci95ub_`var'_ui ci95lb_`var'_ui quarters, ///
		fi(inten50) lw(none) color(forest_green%30)) ///
		(line b_`var'_ui quarters, lcolor(forest_green)) ///
		(scatter b_`var'_ui_sig quarters, ///
		lcolor(forest_green) mcolor(forest_green) msymbol(square)) ///
		(scatter b_`var'_ui_nosig quarters, ///
		lcolor(forest_green) mcolor(forest_green) msymbol(square_hollow)) ///	
		(scatter b_`var'_ui_blank quarters, ///
		lcolor(forest_green) mcolor(forest_green) msize(large) msymbol(square)) ///			
		if quarters>=-5 & quarters<=5, ///
		xline(0, lpattern(solid) lcolor(gs6)) yline(0, lpattern(solid) lcolor(gs6)) ///	
		xtitle("Quarters Since First Receiving Program") ///	
		ytitle("${`var'_ytitle}" "(Relative to 4 Quarters Pre-Receipt)") ///	
		ylabel(, grid gmax gmin) xlabel(-5(1)5) ///		
		legend(order(5 "SNAP (${`var'_snap_mean_round})" 10 "Medicaid (${`var'_med_mean_round})" ///
		15 "UI (${`var'_ui_mean_round})") row(1) pos(6) size(medsmall)) ///
		graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)
	graph export "${`var'_file}.png", replace	
}

*******************
*****Figure 5******
*******************
**** Set up coefficient plots 
foreach var in kid_0to4_any disabled_any sepdivwid_any notworking_any {
	matrix input mat_`var'_q5 = ///
		(${b_`var'_di_q5}, ${b_`var'_wc_q5}, ${b_`var'_ui_q5}, ${b_`var'_snap_q5}, ///
		${b_`var'_tanf_q5}, ${b_`var'_med_q5}, ${b_`var'_ssi_q5}, ${b_`var'_wic_q5} \ /// 		
		${se_`var'_di_q5}, ${se_`var'_wc_q5}, ${se_`var'_ui_q5}, ${se_`var'_snap_q5}, ///
		${se_`var'_tanf_q5}, ${se_`var'_med_q5}, ${se_`var'_ssi_q5}, ${se_`var'_wic_q5})	
}

**** Set globals for titles and file names 
global notworking_any_fig5 "Figure 5a"
global disabled_any_fig5 "Figure 5b"	
global kid_0to4_any_fig5 "Figure 5c"	
global sepdivwid_any_fig5 "Figure 5d"	
			
**** Figures
foreach var in kid_0to4_any disabled_any sepdivwid_any notworking_any {
	coefplot (mat(mat_`var'_sc), ciopts(lcolor(navy)) mcolor(navy) msymbol(circle)), ///
		se(2) xline(0, lcolor(gs8) lpattern(dash)) ///
		xtitle("Percent Change in Outcome (Relative to 4 Quarters Pre-Receipt)") ///
		xlabel(-.4(.2).4) xscale(range(-.45 .45)) ///		
		coeflabel(c1 = "DI (${`var'_di_mean_round})" ///
		c2 = "Workers Comp. (${`var'_wc_mean_round})" ///
		c3 = "UI (${`var'_ui_mean_round})" ///
		c4 = "SNAP (${`var'_snap_mean_round})" ///
		c5 = "TANF (${`var'_tanf_mean_round})" ///
		c6 = "Medicaid (${`var'_med_mean_round})" ///
		c7 = "SSI (${`var'_ssi_mean_round})" ///
		c8 = "WIC (${`var'_wic_mean_round})") legend(off) ///
		graphregion(color(white)) plotregion(fcolor(white) lcolor(white)) scheme(stcolor_alt)
	graph export "${`var'_fig5}.png", replace		
}

*******************
******Table 1******
*******************
clear

foreach prog in di wc ui snap tanf med ssi wic {
	use "sipp_sample_analysis", clear
	
	**** Keep relevant sample
	qui keep if months_since`prog'_min<0 & months_since`prog'_min!=.
	qui keep if (months_since`prog'>=-15 & months_since`prog'<=16)
	qui keep if months_since`prog'==0

	**** New variables 
	gen reason_any = (notworking_any==1 | disabled_any==1 | kid_0to4_any==1 | sepdivwid_any==1)
	
	**** Output 	
	sum tage female thmktinc_pov thallinc_pov unemployed_any nilf_25to59_any ///
		disabled_any kid_0to4_any sepdivwid_any reason_any ///
		di_rec wc_rec ui_rec snap_rec tanf_rec med_rec ssi_rec wic_rec [iw=whfnwgt]
}	
